// pages/accounts/selector/selector.js
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const leadService = require("../../../apiservice/screen/screencustomerservice");

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		source: null,
		scid: "",
		templateid: "",
		scstate: 0,
		searchKey: "",
		height: 0,
		isnextPage: false,
		pagenum: 0,
		total: 0,
		leadid: "",
		type: "",
		//筛查组长
		ownerid: '',
		//筛查项目
		screenitemid: '',
		//筛查类型社区、校园
		screentype: '',
		schoolId: '',
	},

	async GetPage(scid, scstate, conditions, pageNum, pageSize) {

		try {
			var result = await leadService.GetScreenCustomerByScreenIdAndSCStaceAndKeyPaging(scid, scstate, conditions,
				pageNum, 20)

			if (result.data && result.data && result.data.datas) {
				var displaytotal = this.data.displaytotal + result.data.datas.length;
				this.setData({
					['list[' + (pageNum - 1) + ']']: result.data.datas
				});
				this.setData({
					pagenum: pageNum,
					displaytotal: displaytotal,
					isnextPage: result.data.existsnextdata,
					total: result.data.total
				})
				console.log(result.data);
			} else {
				this.setData({
					pagenum: 1,
					displaytotal: 0,
					isnextPage: false,
					total: 0
				})
			}

		} catch (e) {
			//TODO handle the exception

		} finally {
			wx.hideLoading();
		}
	},

	lower: async function() {
		//debugger;
		if (this.data.isnextPage) {
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
				title: '加载中',
				icon: 'loading'
			});
			var pageNum = this.data.pagenum;
			var searhKey = this.data.searchKey
			await this.GetPage(searhKey, ++pageNum, 20);
		} else {
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '我也是有底线的',
				icon: 'success',
				duration: 350
			});
			return false;
		}
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options)
		var res = wx.getSystemInfoSync();
		this.setData({
			height: res.windowHeight,
			scid: options.scid,
			ownerid: options.ownerid,
			screenitemid: options.screenitemid,
		});
		if (options.type) {
			this.setData({
				type: options.type
			})
		}
		if (options.templateid) {
			this.setData({
				templateid: options.templateid
			})
		}
		if (options.screentype) {
			this.setData({
				screentype: options.screentype
			})
		}
		if (options.source) {
			this.setData({
				source: options.source
			})
		}
		if (options.schoolId) {
			this.setData({
				schoolId: options.schoolId
			})
		}
	},

	onSearch: async function(e) {
		// if (!e.detail.searchKey) {
		//   wx.showToast({ //如果全部加载完成了也弹一个框
		//     title: '请输入搜索条件',
		//     icon: 'warn',
		//     duration: 550
		//   });
		//   return false;
		// } else {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});

		this.setData({
			list: null,
			searchKey: e.detail.searchKey
		})

		await this.GetPage(this.data.scid, this.data.scstate, e.detail.searchKey, 1, 20)
		//}


	},
	onNewCustomer: function(e) {
		wx.navigateTo({
			url: '../add/add?new_screenId=' + this.data.scid,
		})

	},
	enterDetail(e) {

		//筛查客户ID
		var sccustid = e.currentTarget.dataset.item.sc_customerid;
		//意向客户ID
		var leadid = e.currentTarget.id;
		//debugger;

		if (this.data.type == "propaganda") {
			let toUrl = '../../screens/propaganda/add/add?source=' +
				this.data.source + '&scid=' + this.data.scid + "&sccustid=" + sccustid + "&leadid=" + leadid +
				"&leadname=" + e.currentTarget.dataset.leadname +
				"&ownerid=" + this.data.ownerid + "&screenitemid=" + this.data.screenitemid;
			if (this.data.schoolId) {
				toUrl += "&schoolId=" + this.data.schoolId
			}
			wx.navigateTo({
				url: toUrl,
				events: {
					// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					acceptDataFromOpenedPage: function(data) {
						console.log(data)
					},
					someEvent: function(data) {
						console.log(data)
					}
				},
				success: function(res) {
					// 通过eventChannel向被打开页面传送数据
					res.eventChannel.emit('acceptDataFromOpenerPage', {
						data: e.currentTarget.dataset.item
					})
				}
			})
		} else if (this.data.type == "result") {
			//潜客id
			//debugger;
			var sccustid = e.currentTarget.id;
			var toUrl = '../../screens/result/type/type?scid=' + this.data.scid + "&sccustid=" +
				sccustid + "&leadid=" + leadid + "&leadname=" +
				e.currentTarget.dataset.leadname +
				"&templateid=" + this.data.templateid;
			if (this.data.schoolId) {
				toUrl += "&schoolId=" + this.data.schoolId
			}
			wx.navigateTo({
				url: toUrl,
				events: {
					// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					acceptDataFromOpenedPage: function(data) {
						console.log(data)
					},
					someEvent: function(data) {
						console.log(data)
					}
				},
				success: function(res) {
					// 通过eventChannel向被打开页面传送数据
					res.eventChannel.emit('acceptDataFromOpenerPage', {
						data: e.currentTarget.dataset.item
					})
				}
			})
		}
		//   console.log(this.data.type);
		// console.log(e.currentTarget.dataset.item.sc_customerid);
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.GetPage(this.data.scid, 0, this.data.searchKey, 1, 20)
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
})
